package com.tanguy.driver.ui.account.adapter;

import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.tanguy.driver.R;
import com.tanguy.driver.base.ImageViewTang;
import com.tanguy.driver.bean.car.CarBrandBean;
import com.tanguy.driver.interfaces.OnItemClickListener;

import java.util.List;

/**
 * Created by dab on 2017/8/14 0014 17:56
 */

public class CarBrandAdapter extends RecyclerView.Adapter<CarBrandAdapter.ViewHolder> {
    private List<CarBrandBean> mCarBrandBeans;
    private OnItemClickListener mOnItemClickListener;
    private int lastSelectPosition = -1;//上次选择的Position

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public void setCarBrandBeans(List<CarBrandBean> carBrandBeans) {
        mCarBrandBeans = carBrandBeans;
        notifyDataSetChanged();
    }


    @Override
    public CarBrandAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_car_brand, parent, false));
    }

    @Override
    public void onBindViewHolder(CarBrandAdapter.ViewHolder holder, int position) {
        CarBrandBean carBrandBean = mCarBrandBeans.get(position);
        holder.mTvName.setText(carBrandBean.getName());
        holder.mIvIcon.setImageURI(carBrandBean.getIcon());
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position, List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);
        if (payloads != null && payloads.size() >= 1) {
            Object o = payloads.get(0);
            if (o instanceof Boolean) {
                int color = (Boolean) o ? R.color.color_fb9a00 : R.color.color_333333;
                holder.mTvName.setTextColor(ContextCompat.getColor(holder.mTvName.getContext(), color));
            }
        }
    }

    public void cancelSelect() {
        if (lastSelectPosition == -1) return;
        notifyItemChanged(lastSelectPosition, false);
    }

    @Override
    public int getItemCount() {
        return mCarBrandBeans == null ? 0 : mCarBrandBeans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView mTvName;
        ImageViewTang mIvIcon;

        public ViewHolder(View itemView) {
            super(itemView);
            mTvName = itemView.findViewById(R.id.tv_name);
            mIvIcon = itemView.findViewById(R.id.sdv_icon);
            if (mOnItemClickListener != null) {
                itemView.setOnClickListener(view -> {
                    mOnItemClickListener.onItemClick(view, getAdapterPosition());
                    notifyItemChanged(getAdapterPosition(), true);
                    lastSelectPosition = getAdapterPosition();
                });

            }
        }
    }


}
